import 'dart:convert';

import 'package:base/main.dart';
import 'package:flutter/services.dart';
import 'package:framework/config/constants.dart';
import 'package:framework/data/model/function_bean.dart';
import 'package:framework/data/model/menu_bean.dart';
import 'package:framework/data/model/stat_bean.dart';
import 'package:framework/data/repositories/home_repository.dart';

/// 具体实现：本地
class HomeLocalRepository implements HomeRepository {
  @override
  Future<List<StatBean>> getDataStatLists() async {
    // 模拟异步操作（如从 API/数据库获取）
    await Future.delayed(const Duration(milliseconds: 1000));

    return [
      StatBean(title: "我的域名", number: 28),
      StatBean(title: "我的预定", number: 71),
      StatBean(title: "我的竞价", number: 66),
      StatBean(title: "急需续费", number: 11),
      StatBean(title: "等待付款", number: 28),
      StatBean(title: "正在出售", number: 71),
    ];
  }

  @override
  Future<List<FunctionBean>> getSystemFunctionLists() async {
    return [
      FunctionBean(title: "域名管理", name: "PUSH域名", icon: "index_5", edit: false),
      FunctionBean(title: "域名管理", name: "交换域名", icon: "index_16", edit: false),
    ];
  }

  @override
  Future<List<FunctionBean>> getFunctionLists() async {
    final data = StorageUtil.read<List>(Constants.STORAGE_FUNCTION_KEY);
    if (data == null) return [];
    return data
        .map((json) =>
            FunctionBean.fromJson((json as Map).cast<String, dynamic>()))
        .toList();
  }

  @override
  Future<List<MenuBean>> getMenuLists() async {
    await Future.delayed(const Duration(milliseconds: 100));
    // 加载本地JSON文件
    final String jsonString =
        await rootBundle.loadString('assets/json/function.json');
    // 解析JSON字符串为List<dynamic>
    final List<dynamic> jsonList = jsonDecode(jsonString);
    // 转换为List<dynamic>
    return jsonList.map((json) => MenuBean.fromJson(json)).toList();
  }

  @override
  Future<List<String>> getQuickBarLists() async {
    // 模拟异步操作（如从 API/数据库获取）
    await Future.delayed(const Duration(milliseconds: 100));
    return ["组 7007@3x", "组 7008@3x", "组 7010@3x", "组 7009@3x"];
  }

  @override
  saveFunctionList(List<FunctionBean> list) {
    final jsonList = list.map((item) => item.toJson()).toList();
    StorageUtil.write(Constants.STORAGE_FUNCTION_KEY, jsonList);
  }

  @override
  Future<List<String>> getBannerLists() async {
    await Future.delayed(const Duration(milliseconds: 100));
    return ["1001", "1001"];
  }
}
